package com.hspedu.houserent.view;

import com.hspedu.houserent.model.House;
import com.hspedu.houserent.service.HouseService;
import com.hspedu.houserent.utils.Utility;

public class HouseView {
    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接收用户选择
    private HouseService houseService = new HouseService(10);

    //完成退出确认
    public void exit(){
        char c = Utility.readConfirmSelection();
        if (c == 'Y'){
            loop = false;
        }
    }


    //编写listHouse()，显示房屋列表
    public void listHouse() {
        System.out.println("============房屋列表===========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();//得到所有房屋信息
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] != null) {//如果不为空，就打印房屋信息
                System.out.println(houses[i]);
            } else {
                break;
            }
        }
        System.out.println("============房屋列表显示完毕===========");
    }


    //添加房屋信息添加功能
    public void addHouse() {
        System.out.println("============添加房屋===========");
        System.out.println("姓名");
        String name = Utility.readString(8);
        System.out.println("电话");
        int phone = Utility.readInt();
        System.out.println("地址");
        String address = Utility.readString(16);
        System.out.println("月租");
        int rent = Utility.readInt();
        System.out.println("状态");
        String state = Utility.readString(3);
        //创建一个新的house对象，id是系统分配的
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("===============添加房屋成功=================");
        } else {
            System.out.println("===============添加房屋失败=================");
        }

    }


    //修改房屋
    public void editHouseById(){
        System.out.println("===============修改房屋=================");
        System.out.println("请选择待修改的房屋编号");
        int editId = Utility.readInt();
//        if (!houseService.find(editId)){
//            System.out.println("没有该房子信息");
//        }
        System.out.println("姓名");
        String name = Utility.readString(8);
        System.out.println("电话");
        int phone = Utility.readInt();
        System.out.println("地址");
        String address = Utility.readString(16);
        System.out.println("月租");
        int rent = Utility.readInt();
        System.out.println("状态");
        String state = Utility.readString(3);
        House editHouse = new House(editId, name, phone, address, rent, state);

        if (houseService.edit(editHouse)) {
            System.out.println("修改成功");
        }else {
            System.out.println("修改失败");
        }
    }


    //房屋信息删除功能,
    public void delHouse() {
        System.out.println("===============删除房屋信息=================");
        System.out.println("请输入代删除的房屋的编号（输入-1退出）：");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("===============删除房屋信息=================");
            return;
        }
        System.out.println("请确认是否删除（Y/N），小心选择：");
        char choise = Utility.readConfirmSelection();
        if (choise == 'Y') {
            if (houseService.del(delId)){
                System.out.println("===============删除房屋信息成功=================");
            }else{
                System.out.println("===============房屋信息不存在，删除失败=================");
            }
        }else{
            System.out.println("===============放弃删除房屋信息=================");
        }

    }


    //根据id查找房屋
    public void findHouseById(){
        System.out.println("===============查找房屋=================");
        System.out.println("请输入你要查找的id：");
        int selectId = Utility.readInt();

        if (!houseService.find(selectId)){
            System.out.println("没有该房子信息");
        }
    }

    //主菜单
    public void mainMenu() {
        do {
            System.out.println("============房屋出租系统菜单===========");
            System.out.println("\t\t\t1 新 增 房 屋");
            System.out.println("\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t3 删 除 房 屋 信 息");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退       出");
            System.out.println("请输入你的选择(1-6)");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    System.out.println("新 增");
                    addHouse();
                    break;
                case '2':
                    System.out.println("查 找");
                    findHouseById();
                    break;
                case '3':
                    System.out.println("删 除");
                    delHouse();
                    break;
                case '4':
                    System.out.println("修 改");
                    editHouseById();
                    break;
                case '5':
                    System.out.println("房 屋 列 表");
                    listHouse();
                    break;
                case '6':
                    System.out.println("退 出");
                    exit();
                    break;
            }
        } while (loop);
    }
}
